﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Ld.Client.Silverlight.LdProxy;
using System.Collections.ObjectModel;

namespace Ld.Client.Silverlight.Views
{
    public partial class ProductView : UserControl
    {
        private App app = Application.Current as App;

        public ProductView()
        {
            InitializeComponent();
        }

        private void btnLoadAll_Click(object sender, RoutedEventArgs e)
        {
            app.Proxy.LoadAllProductAsync();
        }

        public void Proxy_LoadAllProductCompleted(object sender,LoadAllProductCompletedEventArgs e)
        {
            if (e.Error != null)
            {
                return;
            }
            this.grid.ItemsSource = e.Result;
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            app.Proxy.LoadAllProductCompleted += Proxy_LoadAllProductCompleted;

            app.Proxy.ReceiveProductReceived += Proxy_ReceiveProductReceived;
            app.Proxy.ClearProductReceived += Proxy_ClearProductReceived;
        }

        private void UserControl_Unloaded(object sender, RoutedEventArgs e)
        {
            app.Proxy.LoadAllProductCompleted -= Proxy_LoadAllProductCompleted;

            app.Proxy.ReceiveProductReceived -= Proxy_ReceiveProductReceived;
            app.Proxy.ClearProductReceived -= Proxy_ClearProductReceived;
        }

        public void Proxy_ReceiveProductReceived(object sender, ReceiveProductReceivedEventArgs e)
        {
            var list = this.grid.ItemsSource as ObservableCollection<Product>;
            if (list == null)
            {
                return;
            }

            var entity = list.FirstOrDefault(f => f.ID == e.entity.ID);
            if (entity == null)
            {
                list.Add(e.entity);
            }
            else
            {
                entity.Code = e.entity.Code;
                entity.Name = e.entity.Name;
                entity.BuyPrice = e.entity.BuyPrice;
                entity.SellPrice = e.entity.SellPrice;
                entity.QuantityPerUnit = e.entity.QuantityPerUnit;
                entity.Remark = e.entity.Remark;
                entity.Unit = e.entity.Unit;
            }
        }

        public void Proxy_ClearProductReceived(object sender, ClearProductReceivedEventArgs e)
        {
            var list = this.grid.ItemsSource as ObservableCollection<Product>;
            if (list == null)
            {
                return;
            }

            var entity = list.FirstOrDefault(f => f.ID == e.id);
            if (entity == null)
            {
                return;
            }

            list.Remove(entity);
        }

        private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            var entity = new Product { ID = Guid.NewGuid() };
            ProductEdit win = new ProductEdit { DataContext = entity };
            win.Closed += (s, a) =>
            {
                if (win.DialogResult == true)
                {
                    app.Proxy.SaveProductAsync(entity);
                }
            };
            win.Show();
        }

        private void btnUpdate_Click(object sender, RoutedEventArgs e)
        {
            var model = this.grid.SelectedItem as Product;
            if (model == null)
            {
                return;
            }

            var entity = new Product 
            {
                ID = model.ID,
                Code = model.Code,
                Name = model.Name,
                BuyPrice = model.BuyPrice,
                SellPrice = model.SellPrice,
                QuantityPerUnit = model.QuantityPerUnit,
                Remark = model.Remark,
                Unit = model.Unit
            };

            ProductEdit win = new ProductEdit { DataContext = entity };
            win.Closed += (s, a) =>
            {
                if (win.DialogResult == true)
                {
                    app.Proxy.UpdateProductAsync(entity);
                }
            };
            win.Show();
        }

        private void btnDelete_Click(object sender, RoutedEventArgs e)
        {
            if (MessageBox.Show("是否删除此记录", "询问", MessageBoxButton.OKCancel) != MessageBoxResult.OK)
            {
                return;
            }

            var entity = this.grid.SelectedItem as Product;
            if (entity == null)
            {
                return;
            }

            app.Proxy.DeleteProductAsync(entity.ID);
        }
    }
}
